package cqrtplm.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.hustcad.plm.pdm.doc.service.TyplmDocumentService;
import com.hustcad.plm.pdm.flowbusiness.model.entity.FlowObject;
import com.hustcad.plm.pdm.flowbusiness.model.entity.WorkFlowObjectPackage;
import com.hustcad.plm.pdm.iba.service.TyplmAttributeValueService;
import com.hustcad.plm.pdm.workflow.model.entity.WorkFlowContainerView;
import com.hustcad.plm.pdm.workflow.service.TyplmWorkFlowContainerService;
import com.ty.basic.constant.TableTypeConstrant;
import com.ty.basic.handler.exceptionhandler.exception.ResponseCodeEnum;
import com.ty.basic.handler.exceptionhandler.exception.TyException;
import com.ty.basic.vo.AttributeVO;
import cqrtplm.service.RejectTheStatusJudgmentLogicService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author fxw
 * @version 1.0
 * @ClassName RejectTheStatusJudgmentLogicServiceImpl
 * @Description 驳回状态判断逻辑
 * @since 2025/11/6 14:21
 */

@Service
@Slf4j
class RejectTheStatusJudgmentLogicServiceImpl implements RejectTheStatusJudgmentLogicService {
    @Resource
    private TyplmWorkFlowContainerService typlmWorkFlowContainerService;

    @Resource
    private TyplmDocumentService typlmCADDocumentService;
    @Resource
    private TyplmAttributeValueService typlmAttributeValueService;


    @Override
    public void theChangeProcessDoesNotAllowPosting(BigInteger workFlowContainerId) {
        WorkFlowContainerView workFlowContainerView = typlmWorkFlowContainerService.getWorkFlowContainerDetailByPrimaryKey(workFlowContainerId);

        if (ObjectUtils.isEmpty(workFlowContainerView)) {

            TyException.throwSingleTyExceptionVarargs(ResponseCodeEnum.BIZ_PURPOSE_OF_BOM_STATE_ENUM_NOT_FOUND,
                                                      "没找到对应的流程图容器");
        }
        if (workFlowContainerView instanceof WorkFlowObjectPackage) {

            WorkFlowObjectPackage workFlowObjectPackage = (WorkFlowObjectPackage) workFlowContainerView;
            List<FlowObject> flowObjectList = workFlowObjectPackage.getFlowObjectList();
            if (CollUtil.isEmpty(flowObjectList)) {
                TyException.throwSingleTyExceptionVarargs(ResponseCodeEnum.BIZ_PURPOSE_OF_BOM_STATE_ENUM_NOT_FOUND,
                                                          "没找到对应的流程图容器联系管理员");
            }
            List<FlowObject> cadFlowList = new ArrayList<>();
            List<FlowObject> docFlowList = new ArrayList<>();
            Map<String, FlowObject> map = new HashMap<>();
            for(FlowObject flowObject : flowObjectList){
                String refObjectOtype = flowObject.getRefObjectOtype();
                if (TableTypeConstrant.TY_CADDOC_OTYPE.equals(refObjectOtype) ) {
                    map.put(flowObject.getRefObjectOid().toString(), flowObject);
                    cadFlowList.add(flowObject);
                }else if(TableTypeConstrant.TY_DOC_OTYPE.equals(refObjectOtype)){
                    map.put(flowObject.getRefObjectOid().toString(), flowObject);
                    docFlowList.add(flowObject);
                }
            }

            List<BigInteger> docOid = docFlowList.stream().map(FlowObject::getRefObjectOid).collect(Collectors.toList());
            List<BigInteger> cadOid = cadFlowList.stream().map(FlowObject::getRefObjectOid).collect(Collectors.toList());

            Map<String, List<AttributeVO>> docIbaAttrValueByOidAndKeys = typlmAttributeValueService.getIbaAttrValueByOidAndKeys(
                    docOid, TableTypeConstrant.TY_DOC_OTYPE, Collections.singletonList("Stage"));
            Map<String, List<AttributeVO>> cadIbaAttrValueByOidAndKeys = typlmAttributeValueService.getIbaAttrValueByOidAndKeys(
                    cadOid, TableTypeConstrant.TY_CADDOC_OTYPE, Collections.singletonList("Stage"));
            cadIbaAttrValueByOidAndKeys.putAll(docIbaAttrValueByOidAndKeys);

            Set<String> number = new HashSet<>();
            for (Map.Entry<String, List<AttributeVO>> entry : cadIbaAttrValueByOidAndKeys.entrySet()) {
                String key = entry.getKey();
                List<AttributeVO> attrList = entry.getValue();
                if(CollUtil.isNotEmpty(attrList)){
                    AttributeVO attributeVO = attrList.get(0);
                    String attributeValue = attributeVO.getAttributeValue();
                    if(StringUtils.isNotBlank(attributeValue)){
                        FlowObject flowObject = map.get(key);
                        String lifeCycleStageKey = flowObject.getLifeCycleStageKey();
                        if("B".equalsIgnoreCase(attributeValue) && "UNDERCHANGE".equals(lifeCycleStageKey)){
                            number.add(flowObject.getObjectNumber());
                        }
                    }
                }
            }

            if (CollUtil.isNotEmpty(number)) {
                TyException.throwSingleTyExceptionVarargs(ResponseCodeEnum.OBJECT_STATE_CHECK_NOT_PASS,
                                                          "对象" + String.join(",",number) + "不允许通过当前流程发布，请检查！");
            }
        }
    }

}
